<template>
    <div>
      <el-card class="box-card">
        <breadCrumb title="订单列表"></breadCrumb>
        <el-row :gutter="20" style="margin-bottom: 20px;">
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input type="text" placeholder="请输入内容" v-model="query" />
              <el-button class="search" @click="doSearch"><i class="el-icon-search"></i></el-button>
            </div>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column width="50" label="序号" type="index">
          </el-table-column>
          <el-table-column label="订单编号" width="250">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.order_number }}</span>
            </template>
          </el-table-column>
          <el-table-column label="订单价格" width="200">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.order_price }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否付款" width="200">
            <template slot-scope="scope">
              <el-tag :type="+scope.row.order_pay?'success':'danger'">{{ orderPay(scope.row.order_pay) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="是否发货" width="200">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.is_send }}</span>
            </template>
          </el-table-column>
          <el-table-column label="下单时间" width="200">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ formatTime(scope.row.create_time) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit"></el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.goods_id)" icon="el-icon-delete"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="block" style="margin-top: 20px;">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagenum"
            :page-sizes="[2, 4, 6, 8]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-card>
      <el-dialog
        title="修改地址"
        :visible.sync="dialogVisible"
        width="50%"
        >
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
        </el-dialog>
    </div>
  </template>

<script>
import { delGoods } from '@/api/goods'
import { getOrderList } from '@/api/order.js'
import { formatTime } from '@/utils/index.js'
export default {
  name: 'ShoppingGoodsList',

  data () {
    return {
      query: '',
      tableData: [],
      pagenum: 1,
      pagesize: 2,
      total: null,
      isEdit: false,
      formatTime,
      dialogVisible: false
    }
  },

  created () {
    this.goodsList()
  },

  methods: {
    // 是否发货
    orderPay (num) {
      if (+num === 0) {
        return '未付款'
      } else {
        return '已付款'
      }
    },
    async doSearch () {
      const res = await getOrderList({ query: this.query, pagenum: this.pagenum, pagesize: this.pagesize })
      this.total = res.data.total
      this.tableData = res.data.goods
      this.goodsList()
    },
    // 分页器
    handleSizeChange (val) {
      this.pagesize = val
      console.log(`每页 ${val} 条`)
      this.goodsList()
    },
    handleCurrentChange (val) {
      this.pagenum = val
      console.log(`当前页: ${val}`)
      this.goodsList()
    },
    handleEdit (row) {
      this.dialogVisible = true
      console.log(row, '666')
      this.ruleForm = { ...row }
    },
    async handleDelete (id) {
      await delGoods(id)
      if (this.tableData.length === 1) {
        this.pagenum = Math.floor(this.total / this.pagesize)
      }
      this.goodsList()
    },
    async goodsList () {
      const res = await getOrderList({ query: this.query, pagenum: this.pagenum, pagesize: this.pagesize })
      this.total = res.data.total
      console.log(res.data)
      this.tableData = res.data.goods
    }
  }
}
</script>

  <style lang="less" scoped>
  .bg-purple {
    position: relative;

    .search {
      position: absolute;
      top: 0;
      right: 0;
    }
  }
  </style>
